#!/bin/bash

ORIGIN_PATH=$(pwd)
DE_APP_PATH=/home/dev/insight-de
DE_DATA_DIR=/data/insight/log8898
FLUME_BIN_PATH=/home/dev/flume/current/bin
FLUME_POS_FILE=/home/dev/flume/pos/positionFile.json
FLUME_RESTART_TIMES=3
FLUME_LIVE_TIME_IN_SEC=10


function my_echo() {
    local l_day=`date +%F`
    local l_time=`date +%T`
    echo "["$l_day" "$l_time"] "$1
}


#my_echo "Kill existed DE server & client. Wait for 3 seconds..."
#$DE_APP_PATH/killall.sh
#sleep 3s

my_echo "Remove all DE logs in "$DE_DATA_DIR
rm -f $DE_DATA_DIR/*

cd $DE_APP_PATH
my_echo "Start DE server..."
./start_server.sh
my_echo "Start http client..."
./start_client.sh

my_echo "Remove flume position file: "$FLUME_POS_FILE
rm $FLUME_POS_FILE

my_echo "Start the flume app in "$FLUME_BIN_PATH
cd $FLUME_BIN_PATH
./start_flume.sh

for ((i=1; i<=$FLUME_RESTART_TIMES; i++))
do
    my_echo
    my_echo "--"$i"--"
    my_echo "sleep "$FLUME_LIVE_TIME_IN_SEC" seconds..."
    sleep $FLUME_LIVE_TIME_IN_SEC

    my_echo "kill flume"
    ./stop_flume.sh

    my_echo "start flume"
    ./start_flume.sh
done

my_echo

my_echo "kill existed de server & client"
$DE_APP_PATH/killall.sh

my_echo "end."

